/********************************************************************************
 * MailGroupAction.java
 *
 * Created by Jigar Mistry
 * Last modified Date: Aug 29, 2008 4:56:17 PM by Jigar Mistry
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 ********************************************************************************/

package com.printeverywhere.action;

import org.apache.log4j.Logger;
import org.apache.struts2.components.ActionMessage;
import org.hibernate.exception.ConstraintViolationException;

import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import com.printeverywhere.delegator.MailGroupServiceDelegator;
import com.printeverywhere.dto.Groups;

// TODO: Auto-generated Javadoc
/**
 * The Class MailGroupAction.
 */
public class MailGroupAction extends AdminEngineManagement {

	/** Logger for this class. */
	private static final Logger logger = Logger.getLogger(MailGroupAction.class);

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;

	/** The mail group1. */
	private String mailGroup1;

	/** The mail group2. */
	private String mailGroup2;

	/** The mail group3. */
	private String mailGroup3;

	/** The mail group4. */
	private String mailGroup4;

	/** The mail group5. */
	private String mailGroup5;

	/** The group list. */
	private List<Groups> groupList = MailGroupServiceDelegator.getGroupList();

	/** The groups. */
	private String groups[];

	/**
	 * Gets the group list.
	 *
	 * @return the group list
	 */
	public List<Groups> getGroupList() {
		return groupList;
	}

	/**
	 * Sets the group list.
	 *
	 * @param groupList the new group list
	 */
	public void setGroupList(List<Groups> groupList) {
		this.groupList = groupList;
	}

	/**
	 * Gets the groups.
	 *
	 * @return the groups
	 */
	public String[] getGroups() {
		return groups;
	}

	/**
	 * Sets the groups.
	 *
	 * @param groups the new groups
	 */
	public void setGroups(String[] groups) {
		this.groups = groups;
	}

	/**
	 * Gets the mail group1.
	 *
	 * @return the mail group1
	 */
	public String getMailGroup1() {
		return mailGroup1;
	}

	/**
	 * Gets the mail group2.
	 *
	 * @return the mail group2
	 */
	public String getMailGroup2() {
		return mailGroup2;
	}

	/**
	 * Gets the mail group3.
	 *
	 * @return the mail group3
	 */
	public String getMailGroup3() {
		return mailGroup3;
	}

	/**
	 * Gets the mail group4.
	 *
	 * @return the mail group4
	 */
	public String getMailGroup4() {
		return mailGroup4;
	}

	/**
	 * Gets the mail group5.
	 *
	 * @return the mail group5
	 */
	public String getMailGroup5() {
		return mailGroup5;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	public String execute() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start"); //$NON-NLS-1$
		}

		if (logger.isDebugEnabled()) {
			logger.debug("execute() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	/**
	 * Insert or update.
	 *
	 * @return the string
	 */
	public String insertOrUpdate(){
		if (logger.isDebugEnabled()) {
			logger.debug("insertOrUpdate() - start"); //$NON-NLS-1$
		}

		if((mailGroup1 == null || mailGroup1.trim().length() < 1) &&
				(mailGroup2 == null || mailGroup2.trim().length() < 1) &&
				(mailGroup3 == null || mailGroup3.trim().length() < 1) &&
				(mailGroup4 == null || mailGroup4.trim().length() < 1) &&
				(mailGroup5 == null || mailGroup5.trim().length() < 1)) {
			addActionError("Atleast one list name should be there...");
			return INPUT;
		} else {
			if (logger.isDebugEnabled()) {
				logger.debug("insertOrUpdate() - else ... Test..."); //$NON-NLS-1$
			}
			Groups group = new Groups();
			group.setTotalUser(0);

			if(mailGroup1.trim().length() > 45){
				addActionError("First List name must required less than 45 character ");
				return INPUT;
			}else if(mailGroup2.trim().length() > 45){
				addActionError("Second List name must required less than 45 character ");
				return INPUT;
			}else if(mailGroup3.trim().length() > 45){
				addActionError("Third List name must required less than 45 character ");
				return INPUT;
			}else if(mailGroup4.trim().length() > 45){
				addActionError("Fourth List name must required less than 45 character ");
				return INPUT;
			}

			if(mailGroup1 != null && mailGroup1.trim().length() > 1) {
				group.setName(mailGroup1);
				Groups groups1 = MailGroupServiceDelegator.getGroupByName(mailGroup1);
				if(groups1 != null){
					addActionError("First List Name already exist please try another");
					return INPUT;
				}else{
						MailGroupServiceDelegator.insertMailGroup(group);
				}
			}
			if(mailGroup2 != null && mailGroup2.trim().length() > 1) {
				group.setName(mailGroup2);
				Groups groups2 = MailGroupServiceDelegator.getGroupByName(mailGroup2);
				if(groups2 != null){
					addActionError("Second List Name already exist please try another");
					return INPUT;
				}else{
						MailGroupServiceDelegator.insertMailGroup(group);
				}
			}
			if(mailGroup3 != null && mailGroup3.trim().length() > 1) {
				group.setName(mailGroup3);
				Groups groups3 = MailGroupServiceDelegator.getGroupByName(mailGroup3);
				if(groups3 != null){
					addActionError("Third List Name already exist please try another");
					return INPUT;
				}else{
						MailGroupServiceDelegator.insertMailGroup(group);
				}
			}
			if(mailGroup4 != null && mailGroup4.trim().length() > 1) {
				group.setName(mailGroup4);
				Groups groups4 = MailGroupServiceDelegator.getGroupByName(mailGroup4);
				if(groups4 != null){
					addActionError("Forth List Name already exist please try another");
					return INPUT;
				}else{
						MailGroupServiceDelegator.insertMailGroup(group);
				}
			}
			if(mailGroup5 != null && mailGroup5.trim().length() > 1) {
				group.setName(mailGroup5);
				Groups groups5 = MailGroupServiceDelegator.getGroupByName(mailGroup5);
				if(groups5 != null){
					addActionError("Fifth List Name already exist please try another");
					return INPUT;
				}else{
						MailGroupServiceDelegator.insertMailGroup(group);
				}
			}
		}

		if (logger.isDebugEnabled()) {
			logger.debug("insertOrUpdate() - end"); //$NON-NLS-1$
		}
		addActionMessage("New Mailing List has been Created");
		return SUCCESS;
	}

	/**
	 * Show delete list.
	 *
	 * @return the string
	 */
	public String showDeleteList() {
		if (logger.isDebugEnabled()) {
			logger.debug("showDeleteList() - start"); //$NON-NLS-1$
		}

		for (Groups groups : groupList) {
			groups.setName(new StringBuffer(groups.getName()).append(" (").append(MailGroupServiceDelegator.countGroupSubscriberById(groups.getGroupId())).append(")").toString());
		}

		if (logger.isDebugEnabled()) {
			logger.debug("showDeleteList() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	/**
	 * Delete groups.
	 *
	 * @return the string
	 */
	public String deleteGroups() {
		if (logger.isDebugEnabled()) {
			logger.debug("deleteGroups() - start"); //$NON-NLS-1$
		}
		try{
		for (String group : groups) {
			MailGroupServiceDelegator.deleteGroupUser("DELETE from GroupUser groupUser WHERE groupId = " + group);
			MailGroupServiceDelegator.deleteGroup(MailGroupServiceDelegator.getGroupById(Integer.parseInt(group)));
		}
		}catch (NullPointerException e) {
			return INPUT;
		}
		if (logger.isDebugEnabled()) {
			logger.debug("deleteGroups() - end"); //$NON-NLS-1$
		}
		groupList = MailGroupServiceDelegator.getGroupList();
		showDeleteList();
		addActionMessage("Delete selected Mailing List ");
		return SUCCESS;
	}

	/**
	 * Sets the mail group1.
	 *
	 * @param mailGroup1 the new mail group1
	 */
	public void setMailGroup1(String mailGroup1) {
		this.mailGroup1 = mailGroup1;
	}

	/**
	 * Sets the mail group2.
	 *
	 * @param mailGroup2 the new mail group2
	 */
	public void setMailGroup2(String mailGroup2) {
		this.mailGroup2 = mailGroup2;
	}

	/**
	 * Sets the mail group3.
	 *
	 * @param mailGroup3 the new mail group3
	 */
	public void setMailGroup3(String mailGroup3) {
		this.mailGroup3 = mailGroup3;
	}

	/**
	 * Sets the mail group4.
	 *
	 * @param mailGroup4 the new mail group4
	 */
	public void setMailGroup4(String mailGroup4) {
		this.mailGroup4 = mailGroup4;
	}

	/**
	 * Sets the mail group5.
	 *
	 * @param mailGroup5 the new mail group5
	 */
	public void setMailGroup5(String mailGroup5) {
		this.mailGroup5 = mailGroup5;
	}
}